package xin.marcher.wind.migrate.controller;

import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import xin.marcher.wind.migrate.domain.RangeScroll;
import xin.marcher.wind.migrate.domain.dto.EtlProgressReqDTO;
import xin.marcher.wind.migrate.domain.entity.EtlProgressDO;
import xin.marcher.wind.migrate.enums.OperateResult;
import xin.marcher.wind.migrate.migrate.ScrollProcessor;
import xin.marcher.wind.migrate.service.MigrateConfigService;
import xin.marcher.wind.migrate.service.MigrateService;

import javax.annotation.Resource;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据同步迁移配置入口
 */
@RestController
@RequestMapping("/v1/migrate")
public class MigrateController {

    @Resource
    private MigrateService migrateService;

    @Resource
    MigrateConfigService migrateConfigService;

    @Resource
    private ScrollProcessor scrollProcessor;

    /**
     * 取得迁移进度信息
     *
     * @param request 查询条件
     */
    @PostMapping(value = "/progresses")
    public Map<String, Object> getEtlProgresses(@RequestBody EtlProgressReqDTO request) {
        EtlProgressDO etlProgressDO = new EtlProgressDO();
        BeanUtils.copyProperties(request, etlProgressDO);
        List<EtlProgressDO> resultList = migrateService.getEtlProgresses(etlProgressDO);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("resultCode", OperateResult.SUCCESS.getValue());
        resultMap.put("resultMsg", OperateResult.SUCCESS.getName());
        resultMap.put("resultList", resultList);
        return resultMap;
    }

    /**
     * 补偿失败的范围滚动拉取数据
     *
     * @param id 同步记录 ID
     */
    @PostMapping("/resync")
    public Map<String, Object> reSync(Long id, String domain) {
        migrateService.compensateRangeScroll(id, domain);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("resultCode", OperateResult.SUCCESS.getValue());
        resultMap.put("resultMsg", OperateResult.SUCCESS.getName());
        return resultMap;
    }

    /**
     * 新增全量同步 将前端传过来的时间格式化
     *
     * @param rangeScroll 全量同步条件
     * @return 保存结果
     */
    @PostMapping("/add-scroll")
    public Map<String, Object> addScroll(@RequestBody RangeScroll rangeScroll) {
        rangeScroll.setStartTime(rangeScroll.getStartTime().toLocalDate().atStartOfDay());
        rangeScroll.setEndTime(rangeScroll.getEndTime().toLocalDate().atTime(LocalTime.MAX));
        scrollProcessor.scroll(rangeScroll);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("resultCode", OperateResult.SUCCESS.getValue());
        resultMap.put("resultMsg", OperateResult.SUCCESS.getName());
        return resultMap;
    }

    /**
     * 通过业务名获取表名
     *
     * @param domain 业务名
     * @return 响应
     */
    @GetMapping("/tables")
    public Map<String, Object> getTableNameToDomain(@RequestParam("domain") String domain) {
        List<String> resultList = migrateConfigService.getTablesToDomain(domain);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("resultCode", OperateResult.SUCCESS.getValue());
        resultMap.put("resultMsg", OperateResult.SUCCESS.getName());
        resultMap.put("resultList", resultList);
        return resultMap;
    }
}
